#!/usr/bin/python3 -cimport os, sys; os.execv(os.path.dirname(sys.argv[1]) + "/../common/pywrap", sys.argv)

# This file is part of Cockpit.
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Cockpit is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Cockpit is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Cockpit; If not, see <https://www.gnu.org/licenses/>.

import storagelib
import testlib


@testlib.nondestructive
class TestStorageMsDOS(storagelib.StorageCase):

    def testDosParts(self):
        b = self.browser

        self.login_and_go("/storage")

        # Add a disk
        disk = self.add_ram_disk()
        self.click_card_row("Storage", name=disk)

        # Format it with a DOS partition table
        self.click_card_dropdown("Solid State Drive", "Create partition table")
        self.dialog({"type": "dos"})
        b.wait_text(self.card_row_col("DOS partitions", 1, 1), "Free space")

        # Create a primary partition
        self.click_dropdown(self.card_row("DOS partitions", 1), "Create partition")
        self.dialog({"size": 10,
                     "type": "ext4",
                     "mount_point": f"{self.mnt_dir}/foo",
                     "name": "FIRST"},
                    secondary=True)
        b.wait_text(self.card_row_col("DOS partitions", 1, 2), "ext4 filesystem")

        # Open dialog for formatting the primary partition and check that "dos-extended" is not offered.
        self.click_card_row("DOS partitions", 1)
        self.click_card_dropdown("ext4 filesystem", "Format")
        self.dialog_wait_open()
        b.wait_not_present("select option[value='dos-extended']")
        self.dialog_cancel()
        self.dialog_wait_close()
        b.click(self.card_parent_link())

        # Create a extended partition to fill the rest of the disk
        self.click_dropdown(self.card_row("DOS partitions", 2), "Create partition")
        self.dialog_wait_open()
        self.dialog_set_val("type", "dos-extended")
        self.dialog_wait_not_present("name")
        self.dialog_wait_not_present("mount_point")
        self.dialog_wait_not_present("mount_options")
        self.dialog_apply()
        self.dialog_wait_close()
        b.wait_text(self.card_row_col("DOS partitions", 2, 1), "Extended partition")
        b.wait_text(self.card_row_col("DOS partitions", 3, 1), "Free space")

        # Create logical partitions and check that "dos-extended" is
        # not offered.
        self.click_dropdown(self.card_row("DOS partitions", 3), "Create partition")
        self.dialog_wait_open()
        b.wait_not_present("select option[value='dos-extended']")
        self.dialog_cancel()
        self.dialog_wait_close()

        # Delete it

        self.click_dropdown(self.card_row("DOS partitions", 2), "Delete")
        self.confirm()

        b.wait_text(self.card_row_col("DOS partitions", 2, 1), "Free space")


if __name__ == '__main__':
    testlib.test_main()
